import java.util.HashMap;

public class Solution {
    /*两数之和*/
    /*哈希映射*/
    public static int[] twoSum_new(int[] nums, int target) {
        HashMap<Integer,Integer> hash = new HashMap<>();
        int n = nums.length;
        for(int i = 0; i < n; i++) {
            if(hash.containsKey(target - nums[i])) {
                return new int[]{i, hash.get(target - nums[i])};
            }
            hash.put(nums[i],i);

        }
        return new int[]{};
    }
    /*移动零*/
    public void moveZeroes_in(int[] nums) {
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            //i走到0位置
            if(nums[i] == 0) {
                for(int j = i+1; j < n; j++) {
                    if(nums[j] != 0) {
                        int tmp = nums[i];
                        nums[i] = nums[j];
                        nums[j] = tmp;
                        break;
                    }
                }
            }
        }
    }
    /*移动零，优化*/
    public void moveZeroes(int[] nums) {
        int left = 0;
        int right = 0;
        int n = nums.length;
        while (right < n) {
            /*right始终移动到非零位置，然后与第一个0位置进行交换*/
            if(nums[right] != 0) {
                int tmp = nums[right];
                nums[right] = nums[left];
                nums[left] = tmp;
                left++;
            }
            right++;
        }
    }
}
